package starComponent.elements;

import java.awt.FontMetrics;


/**
 * @author Charles Perin
 * 11 avr. 2011
 * LIMSI - INRIA Rennes
 * charles.perin@free.fr
 */
public class LadderTick {
	
	public static int TICK_WIDTH=2;
	public static int TICK_HEIGHT=10;
	

	protected double value;
	protected String label;
	protected int labelWidth, labelHeight;

	protected int xCenter, yCenter, xLabel, yLabel, xCenterLabel, yCenterLabel;

	public static final int RADIUS = 5;
	protected static final int MARGIN_LABEL = 10;

	protected A_StarBranch branch;

	public LadderTick(A_StarBranch branch, double value, String label){
		this.branch = branch;
		FontMetrics fm = branch.getStar().getGraphics().getFontMetrics();
		labelWidth = fm.stringWidth(label);
		labelHeight = fm.getHeight();
		setValue(value);
		setLabel(label);
		calculateTickCenter();
		calculateLabelPosition();
	}
	
	@Override
	public String toString(){
		return "val="+value+", label="+label+", center=("+xCenter+","+yCenter+"), label=("+xLabel+","+yLabel+")";
	}

	protected void calculateTickCenter(){
		if(branch.getLadder().getMode()==Ladder.LINEAR){
			xCenter = (int) (value*branch.getLength()*Math.cos(branch.getAngle())/branch.getMax()+branch.getxStart());
			yCenter = (int) (-value*branch.getLength()*Math.sin(branch.getAngle())/branch.getMax()+branch.getyStart());
		}
		else if(branch.getLadder().getMode()==Ladder.LOGARITHMIC){
			xCenter = (int) (Math.log10(value)*branch.getLength()*Math.cos(branch.getAngle())/Math.log10(branch.getMax())+branch.getxStart());
			yCenter = (int) (-Math.log10(value)*branch.getLength()*Math.sin(branch.getAngle())/Math.log10(branch.getMax())+branch.getyStart());
		}
	}

	protected void calculateLabelPosition(){
		
		xLabel = (int) ((value)*branch.getLength()*Math.cos(branch.getAngle())/branch.getMax()+branch.getxStart()-labelWidth/2);
		
		if(branch.getAngle()>Math.PI)
			yLabel = (int) (-(value)*branch.getLength()*Math.sin(branch.getAngle())/branch.getMax()+branch.getyStart()-labelHeight);
		else
			yLabel = (int) (-(value)*branch.getLength()*Math.sin(branch.getAngle())/branch.getMax()+branch.getyStart()+labelHeight);
		
		xCenterLabel = xLabel + labelWidth/2;
		yCenterLabel = yLabel + labelHeight/2;

	}


	public double getValue() {
		if(branch.getLadder().getMode()==Ladder.LINEAR){
			return value;
		}
		else if(branch.getLadder().getMode()==Ladder.LOGARITHMIC){
			return Math.log10(value);
		}
		return value;
	}

	public void setValue(double value) {
		this.value = value;
	}

	public String getLabel() {
		return label;
	}

	public void setLabel(String label) {
		this.label = label;
	}

	public int getxCenter() {
		return xCenter;
	}

	public void setxCenter(int xCenter) {
		this.xCenter = xCenter;
	}

	public int getyCenter() {
		return yCenter;
	}

	public void setyCenter(int yCenter) {
		this.yCenter = yCenter;
	}

	public int getxLabel() {
		return xLabel;
	}

	public void setxLabel(int xLabel) {
		this.xLabel = xLabel;
	}

	public int getyLabel() {
		return yLabel;
	}

	public void setyLabel(int yLabel) {
		this.yLabel = yLabel;
	}


	public void setxCenterLabel(int xCenterLabel) {
		this.xCenterLabel = xCenterLabel;
	}

	public int getxCenterLabel() {
		return xCenterLabel;
	}

	public void setyCenterLabel(int yCenterLabel) {
		this.yCenterLabel = yCenterLabel;
	}

	public int getyCenterLabel() {
		return yCenterLabel;
	}
}
